﻿package com.devilcat.ui{
	import flash.display.MovieClip;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.ui.Keyboard;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import com.devilcat.ob.NPCSell;
	import com.devilcat.ws.*;
	import com.devilcat.dt.Gamedt;
	//import com.devilcat.PVPBattle;
	import com.devilcat.ob.NPCPokemon;
	import com.devilcat.ob.NPCMessenger;
	import com.devilcat.ui.Battleui;
	import com.devilcat.ui.PokemonListui;
	import com.devilcat.dt.Pokemondt;

	//import starling.core.*;

	public class Mainui extends MovieClip
	{
		var mapPokemon:XMLList;
		var mapEvent:XMLList;
		var curentMap:String = "SheepRanch";
		var newMap:String = "SheepRanch";
		var totalPokemonInMap:Number;
		var wildPokemonList:Array;
		var tweenList:Array;
		var menuIsShowed:Boolean = true;
		var isShowPokemon:Boolean = false;
		var rankIsShowed:Boolean = false;
		var mapswapIsShowed:Boolean = false;
		var pokedexIsShowed:Boolean = false;
		var pokemonIsShowed:Boolean = false;
		var PVPIsShowed:Boolean = false;
		var itemIsShowed:Boolean = false;
		var eventWalkingIsAdded:Boolean = false;
		var trainerIsStanding:Boolean = true;
		var trainerIsJumping:Boolean = false;
		var trainerIsGoingLeft:Boolean = false;
		var trainerIsGoingRight:Boolean = false;
		var pixelCount:Number = 0;
		var speed:Number = 0;
		var speedPow:Number = 7;
		var friction = 0.9;
		var tokenCheck:uint;		
		
		//var gameEventList:Array=new Array();
		var NPCUpMessenger:Array;
		var NPCUpBattle:Array;
		var NPCMustBattle:Array;
		var npcEventList:Array;
		var intervalId:uint;
		var tokenws:UserIntervalws=new UserIntervalws();
		var playerws:LoadPlayerListws=new LoadPlayerListws();
		public function Mainui()
		{
			
//			Starling.handleLostContext=true;
//			var st:Starling = new Starling(HelloFeathers, this.stage);
//			st.showStats = true;
//			st.start();

			Gamedt.MainUI = this;		
//			Gamedt.PokemonListUI=GameMenu.PokemonPanel;
//			Gamedt.ItemListUI=GameMenu.ItemPanel;
			preLoadtxt.text = "Loading...";
			LoginForm.visible=false;
			addEventListener(Event.ENTER_FRAME,preLoad);
			stop();
		}

		function Trace(st:String)
		{
			traceText.text = st + "\n" + traceText.text;
		}
		function preLoad(evt:Event)
		{
			var total:Number = loaderInfo.bytesTotal;
			var loaded:Number = loaderInfo.bytesLoaded;
			preLoadtxt.text=Math.round(loaded/total*100).toString()+"%";
			if (total==loaded)
			{
				removeEventListener(Event.ENTER_FRAME,preLoad);
				preLoadtxt.text = "Loading Moves...";
				loadXML("Moves.xml",loadXMLMoves);
			}
		}
		function loadXML(link:String, myfun:Function)
		{
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.dataFormat = new XML  ;
			xmlLoader.load(new URLRequest(link));
			xmlLoader.addEventListener(Event.COMPLETE, myfun);
		}
		function loadXMLMoves(evt:Event)
		{
			Gamedt.movesXML = new XML(evt.target.data);
			preLoadtxt.text = "Loading Pokemon...";
			//loadXML("ItemList.xml?"+Math.round(Math.random()*1000),loadXMLItems);
			loadXML("ItemList.xml",loadXMLItems);
		}
		function loadXMLItems(evt:Event)
		{
			Gamedt.itemsXML = new XML(evt.target.data);
			preLoadtxt.text = "Loading Item...";
			loadXML("Pokemons.xml",loadXMLPokemons);
		}
		function loadXMLPokemons(evt:Event)
		{
			Gamedt.pokemonsXML = new XML(evt.target.data);
			preLoadtxt.text = "Loading Map...";
			//loadXML("twalk_xmldata.php?xdata=MapPokemons",loadXMLMaps);
			loadXML("MapPokemons.xml",loadXMLMaps);
		}
		function loadXMLMaps(evt:Event)
		{
			Gamedt.mapsXML = new XML(evt.target.data);
			mapEvent = Gamedt.mapsXML.MapEvent;
			preLoadtxt.text = "";
			LoginForm.visible=true;
			LoginForm.LoginButton.addEventListener(MouseEvent.CLICK,DoLoginForm);
			function DoLoginForm(e:MouseEvent){
				preLoadtxt.text = "Loging......";
				var wTest:Loginws=new Loginws(LoginForm.txtUsername.text,LoginForm.txtPassword.text)
			}
		}
		function loadUserPokemonList(){
			
		}
		public function GameStart(){
			if (Gamedt.Player.map==""){
				curentMap = "SheepRanch";
				newMap = "SheepRanch";
			}else{
				curentMap = Gamedt.Player.map;
				newMap = Gamedt.Player.map;
			}
			gotoAndStop("game");
			initGame();
		}


		public function initGame()
		{
			trace("initGame");
			
			Gamedt.updateItem = new UpdateItemws();
			Gamedt.updatePlayer = new UpdatePlayerInforws();
			Gamedt.updatePokemon = new UpdatePokemonws();
			Gamedt.updateEvent = new UpdateUserEventws();
			GameMenu.MapSwapPanel.x = 130;
			GameMenu.PokedexPanel.x = 130;
			GameMenu.PokemonPanel.x = 130;
			GameMenu.PVPPanel.x = 130;
			GameMenu.pvpMask.visible=false;	
			endpvpbtn.pvpcancel.addEventListener(MouseEvent.CLICK,clearPVPEvent);
			Trainer.gotoAndPlay("standing");
			mapSwap(curentMap,Gamedt.Player.miles,false,false);
			enableAllMenu();
			tokenCheck = setInterval(checkMultiTab,1000);//SQL
			setNewInforPanel();
		}
		function mapSwap(mapName:String, miles:Number, mapEvent:Boolean=false, reco:Boolean=true)
		{
			trace("Map: " +mapName)
			mapPokemon = Gamedt.mapsXML[mapName];
			totalPokemonInMap = mapPokemon.pkml.pkm.length();
			newMap = mapName;
			Gamedt.OtherPlayerList=new Array();
			addWordNPC(miles);
			if (! mapEvent)
			{
				Gamedt.Player.miles = miles;
				curentMap = mapName;
				if (reco)
				{
					RecoveryAllPokemon();
				}
			}
			freeTween();
			tweenList.unshift(new Tween (RootWorldObject,"y",Strong.easeOut,RootWorldObject.y,RootWorldObject.y+300,0.5,true));
			tweenList.unshift(new Tween (RootWorldObject,"alpha",Strong.easeOut,1,0,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,gotoNewMap);
			function gotoNewMap(evt:Event)
			{
				pixelCount = 0;
				RootWorldObject.x = 0;
				RootWorldObject.gotoAndStop(mapName);
				tweenList.unshift(new Tween (RootWorldObject,"y",Strong.easeOut,RootWorldObject.y,179,0.5,true));
				tweenList.unshift(new Tween (RootGround,"x",Strong.easeOut,RootGround.x,0,0.5,true));
				tweenList.unshift(new Tween (RootWorldObject,"alpha",Strong.easeOut,0,1,1,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,enableMenuEvent);				
				Gamedt.updatePlayer.Update("map",curentMap);
				Gamedt.updatePlayer.Update("miles",Gamedt.Player.miles);
				Gamedt.updatePlayer.Update("status",1);
				addEventWalking();
			}
		}
		function addEventWalking()
		{
			trace("addEventWalking");
			stage.addEventListener(KeyboardEvent.KEY_DOWN,getKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,getKeyUpEvent);
		}
		function getNPCList(mile:Number,XMLData:XMLList, type:String):Array{
			var ArrayObject=new Array();
			var EventL:String;
			var EventN:Number;
			for (var i:int=0; i<XMLData.children().length(); i++){
				EventL = String(XMLData.child(i).el);
				EventN = Number(XMLData.child(i).en);
			if (Number(XMLData.child(i).mile)==mile && Gamedt.Player.checkEvent(EventL,EventN))
				{
					var npc;
					switch (type){
						case "NPCPokemon":
							npc = new NPCPokemon(XMLData.child(i));
							trace("New NPC");
							break
						case "NPCMessenger":						
							npc = new NPCMessenger(XMLData.child(i));		
							break;
					}
					ArrayObject.push(npc);
					npc.x = 500 + Number(XMLData.child(i).xpos);
					RootWorldObject.NPCObject.addChild(npc);
					npc.addMe();
				}			
			}	
			return ArrayObject;
		}

		function addWordNPC(mile:Number)
		{
			while (RootWorldObject.NPCObject.numChildren > 0)
			{
				RootWorldObject.NPCObject.removeChildAt(0);
			}
			NPCUpMessenger = getNPCList(mile,mapPokemon.npcchat,"NPCMessenger");
			NPCMustBattle = getNPCList(mile,mapPokemon.trainer,"NPCPokemon");
			NPCUpBattle = getNPCList(mile,mapPokemon.pkmevt,"NPCPokemon");
	//		npcEventList = getNPCList(mile,mapPokemon.npcchat,false);
			var npseller:NPCSell;
			if(String(mapPokemon.seller)=="")
				npseller=new NPCSell(Gamedt.mapsXML.seller);	
			else		
				npseller=new NPCSell(mapPokemon.seller);
			RootWorldObject.NPCObject.addChild(npseller);
			npseller.addMe();
		}
		function worldObjectMoving(e:Event)
		{
			if ( !(speed<0 && pixelCount<=6 && Gamedt.Player.miles<=1)  )
			{
				//if(Gamedt.Player.miles>0){
				for (var i:int =0; i < NPCMustBattle.length; i++)
				{
					if (Trainer.hitTestObject(NPCMustBattle[i]))
					{
						if (menuIsShowed)
						{
							showMenu();
						}
						//loadBattle(NPCMustBattle[i].Pokemon,true);
						NPCMustBattle[i].hitMe();
						BeforeBattle();
						var newBattleUI = new Battleui(NPCMustBattle[i].pokemonList,true);
						if(NPCMustBattle[i].eventLocation!="") newBattleUI.SetEventBattle(NPCMustBattle[i].eventLocation,NPCMustBattle[i].eventNumber);
						BlankMC.addChild(newBattleUI);
						break;
					}
				}
				pixelCount +=  speed;
				RootGround.x -=  speed;
				RootWorldObject.x -=  speed;
				if ((pixelCount>3234 || pixelCount<0))
				{
					if (newMap!=curentMap)
					{
						mapSwap(curentMap,Gamedt.Player.miles,false);
					}
					else
					{
						if (pixelCount>3234)
						{
							pixelCount = 0;
							RootGround.x = 0;
							RootWorldObject.x = 0;
							Gamedt.Player.miles++;
						}
						else
						{
							pixelCount = 3234;
							RootGround.x = -3234;
							RootWorldObject.x = -3234;
							Gamedt.Player.miles--;
						}
						Gamedt.updatePlayer.Update("map",curentMap);
						Gamedt.updatePlayer.Update("miles",Gamedt.Player.miles);
						//SQLData.ChangeMap(curentMap,Gamedt.Player.miles);
						//UpdateWS
						setNewInforPanel();
						addWordNPC(Gamedt.Player.miles);
					}
				}
			}
		}
		function addPokemonAuto()
		{
			if(totalPokemonInMap>0){
				var randomPokemonNumber = Math.round(Math.random() * (totalPokemonInMap - 1));
				if (wildPokemonAppear(mapPokemon.pkml.child(randomPokemonNumber).wrate))
				{
					wildPokemonList = new Array();
					wildPokemonList.push(Gamedt.newWildPokemon(mapPokemon.pkml.child(randomPokemonNumber)));
					BeforeBattle();
					BlankMC.addChild(new Battleui(wildPokemonList,false,Gamedt.Player.miles));
				}
			}
		}
		function addNewFloatText(str:String,xx:Number,yy:Number)
		{
			addChild(new FloatingText(str,xx,yy));
		}
		function setNewInforPanel()
		{
			//Trace("Set! "+SQLData.pmp);
			GameMenu.PMP.text = String(Gamedt.Player.gold);
			GameMenu.Miles.text = String(Gamedt.Player.miles);
		}

		function removeEventWalking()
		{
			if (menuIsShowed)
			{
				showMenu();
			}
			//Trace("removeEventWalking");
			clearInterval(intervalId);
			Trainer.gotoAndPlay("standing");
			trainerIsStanding = true;
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,getKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,getKeyUpEvent);
			removeEventListener(Event.ENTER_FRAME,worldObjectMoving);
		}
	

		function getKeyUpEvent(evt:KeyboardEvent)
		{
			getKeyUp();
		}
		function getKeyUp()
		{
			clearInterval(intervalId);
			Trainer.gotoAndPlay("standing");
			if(!trainerIsStanding)
				Gamedt.updatePlayer.Update("status",1);
			trainerIsStanding = true;
			removeEventListener(Event.ENTER_FRAME,worldObjectMoving);
		}
		function getKeyDown(evt:KeyboardEvent)
		{
			if(evt.keyCode==Keyboard.UP || evt.keyCode==Keyboard.RIGHT || evt.keyCode==Keyboard.LEFT){
			var j:int;
			var i:int;
			if (trainerIsStanding && evt.keyCode!=Keyboard.UP)
			{
				addEventListener(Event.ENTER_FRAME,worldObjectMoving);
				intervalId = setInterval(addPokemonAuto,2000);
				Trainer.gotoAndPlay("walking");
				trainerIsStanding = false;
				if (Math.round(Math.random() * 10) == 1)
				{
					addPokemonAuto();
				}
//				for (j =0; j < npcEventList.length; j++)
//				{
//					npcEventList[j].unHitMe();
//				}
			}
			switch (evt.keyCode)
			{
				case Keyboard.LEFT :
					speed =  -  speedPow;
					if(!trainerIsGoingLeft)					
						Gamedt.updatePlayer.Update("status",2);
					trainerIsGoingLeft = true;
					trainerIsGoingRight = false;
					Trainer.scaleX = -1;
						
					break;
				case Keyboard.RIGHT :
					speed = speedPow;
					if(!trainerIsGoingRight)			
						Gamedt.updatePlayer.Update("status",3);
					trainerIsGoingLeft = false;
					trainerIsGoingRight = true;
					Trainer.scaleX = 1;
					break;
				case Keyboard.UP :
					speed = 0;
					getKeyUp();
					for (i =0; i < NPCUpMessenger.length; i++)
					{
						if (Trainer.hitTestObject(NPCUpMessenger[i]))
						{
//							if (menuIsShowed)
//							{
//								showMenu();
//							}
							NPCUpMessenger[i].hitMe();
							break;
						}
					}
					for (i =0; i < NPCUpBattle.length; i++)
					{
						if (Trainer.hitTestObject(NPCUpBattle[i]))
						{
							if (menuIsShowed)
							{
								showMenu();
							}
							NPCUpBattle[i].hitMe();
							BeforeBattle();
							var newBattleUI = new Battleui(NPCUpBattle[i].pokemonList);
							if(NPCUpBattle[i].eventLocation!="") newBattleUI.SetEventBattle(NPCUpBattle[i].eventLocation,NPCUpBattle[i].eventNumber);
							BlankMC.addChild(newBattleUI);
							break;
						}
					}
			}
			}
		}
					
		/////////////////////////////////Function

		function freeTween()
		{
			tweenList = null;
			tweenList=new Array();
		}
		function checkMultiTab()
		{
			tokenws.checkToken();
			playerws.Update(Gamedt.Player.miles);
			if(trainerIsGoingLeft)
				Gamedt.updatePlayer.Update("status",2);
			if(trainerIsGoingRight)
				Gamedt.updatePlayer.Update("status",3);
			if(trainerIsStanding)
				Gamedt.updatePlayer.Update("status",1);
		}
		public function errorToken(){
			gotoAndStop("flag");
			clearInterval(tokenCheck);	
			removeEventWalking();
			clearInterval(tokenCheck);			
		}

		function wildPokemonAppear(wrate:String):Boolean
		{
			var aRate:Number;
			if (wrate=="")
			{
				aRate = 90;
			}
			else
			{
				aRate = Number(wrate);
			}
			if (notMiss(aRate))
			{
				return true;
			}
			return false;
		}

		function notMiss(Accuracy:Number):Boolean
		{
			if (Math.round(Math.random() * 100) > Accuracy)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		public function BeforeBattle()
		{
			Gamedt.updatePlayer.Update("status",1);
			hideAllPanel();
			disableAllMenu();
			removeEventWalking();
		}
		public function AfterBattle(_case:String)
		{
				//Trace("...EnableEventAfterBattle....");
				
			GameMenu.pvpMask.visible=false;
			GameMenu.pvpMask.x=1000;
			enableAllMenu();
			Gamedt.Player.setHPLead();
			switch (_case){
				case "pvp":
					addEventWalking();	
					//Trainer.addChild(new FloatingText("",0,0,2));
					break;
				case "catched":
					addEventWalking();	
					Trainer.addChild(new FloatingText("BO CHAY...",0,0,2));
					//reloadMode = "insert";
					//SQLData.GetData("Login",ReloadPokemonList);		
					break;
				case "flee":
					addEventWalking();	
					Trainer.addChild(new FloatingText("BO CHAY...",0,0,2));
					break;
				case "win":
					addEventWalking();			
					Trainer.addChild(new FloatingText("CHIEN THANG!",0,0,2));	
					break;
				case "failed":
					Trainer.addChild(new FloatingText("BAN DA THUA!",0,0,2));
					mapSwap(curentMap,1,false);
					break;
			}
		}
		function loadBattle(rivalPokemonList:Array,isTrainer:Boolean=false)
		{
			//BeforeBattle();
			//BlankMC.addChild(new PokemonBattle(rivalPokemonList,isTrainer));
		}
		function getPokemonIDFromName(pokemonName:String):Number
		{
			return Number(Gamedt.pokemonsXML[pokemonName].No);
		}

		function enableAllMenu()
		{
			ShowAllMenu.addEventListener(MouseEvent.CLICK,showMenuEvent);
		}
		function disableAllMenu()
		{
			ShowAllMenu.removeEventListener(MouseEvent.CLICK,showMenuEvent);
		}
		function RecoveryAllPokemon()
		{
			for (var i:int=0; i<Gamedt.Player.pokemonList.length; i++)
			{
				Gamedt.Player.pokemonList[i].Recovery();
				Gamedt.updatePokemon.Update(Gamedt.Player.pokemonList[i]);
			}
		}
		function removeAllEvent()
		{
			disableMenu();
		}

		function loadRank()
		{
			GameMenu.RankPanel.GridData.removeAllColumns();
			GameMenu.RankPanel.GridData.dataProvider.removeAll();
			GameMenu.RankPanel.GridData.addColumn("Trainer");
			GameMenu.RankPanel.GridData.addItem({Trainer:"DragonAce"});
		}
		function enableMenuEvent(e:Event)
		{
			trace("Swap Ok!");
			enableMenu();
		}
		function enableMenu()
		{
			GameMenu.ShowRank.addEventListener(MouseEvent.CLICK,showRankEvent);
			GameMenu.ShowPokedex.addEventListener(MouseEvent.CLICK,showPokedexEvent);
			GameMenu.ShowMapSwap.addEventListener(MouseEvent.CLICK,showMapSwapEvent);
			GameMenu.ShowPokemon.addEventListener(MouseEvent.CLICK,showPokemonEvent);
			GameMenu.ShowPVP.addEventListener(MouseEvent.CLICK,showPVPEvent);
			GameMenu.ShowItem.addEventListener(MouseEvent.CLICK,showItemEvent);

			GameMenu.MapSwapPanel.addEventListener(MouseEvent.CLICK,clickMapSwap);
		}
		function disableMenu()
		{
			GameMenu.ShowRank.removeEventListener(MouseEvent.CLICK,showRankEvent);
			GameMenu.ShowPokedex.removeEventListener(MouseEvent.CLICK,showPokedexEvent);
			GameMenu.ShowMapSwap.removeEventListener(MouseEvent.CLICK,showMapSwapEvent);
			GameMenu.ShowPokemon.removeEventListener(MouseEvent.CLICK,showPokemonEvent);
			GameMenu.ShowPVP.removeEventListener(MouseEvent.CLICK,showPVPEvent);
			GameMenu.ShowItem.removeEventListener(MouseEvent.CLICK,showItemEvent);

			GameMenu.MapSwapPanel.removeEventListener(MouseEvent.CLICK,clickMapSwap);
		}
		function gameMenu(evt:Event)
		{
			if (menuIsShowed)
			{
			}
			else
			{
			}
		}
		function hideAllPanel()
		{
			
			Gamedt.pvpAI=false;
			hidePVPPanel();
			hideItemPanel();
			hidePokemonPanel();
			hidePokedexPanel();
			hideRankPanel();
			hideMapSwap();
		}
		function clickMapSwap(evt:MouseEvent)
		{
			mapSwap(evt.target.name,1,false);
		}
		function showMenuEvent(evt:Event)
		{
			showMenuManual();
		}
		function showMenuManual()
		{
			showMenu();
		}
		function showMenu()
		{
			if (! menuIsShowed)
			{
				setNewInforPanel();
				enableMenu();
				ShowAllMenu.scaleX = -1;
				ShowAllMenu.x = 40;
				freeTween();
				tweenList.unshift(new Tween (GameMenu,"x",Strong.easeOut,-135,8,1,true));
				tweenList.unshift(new Tween (GameMenu,"alpha",Strong.easeOut,0,1,1,true));
				tweenList.unshift(new Tween (ShowAllMenu,"y",Strong.easeOut,4,182,1,true));
				menuIsShowed = true;
			}
			else
			{
				disableMenu();
				ShowAllMenu.scaleX = 1;
				ShowAllMenu.x = 4;
				tweenList.unshift(new Tween (GameMenu,"x",Strong.easeOut,8,-135,1,true));
				tweenList.unshift(new Tween (GameMenu,"alpha",Strong.easeOut,1,0, 1,true));
				tweenList.unshift(new Tween (ShowAllMenu,"y",Strong.easeOut,182,0, 1,true));
				hideAllPanel();
				menuIsShowed = false;
			}
		}
		function showMapSwap()
		{
			if (! mapswapIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.MapSwapPanel,"y",Strong.easeOut,-272,-2,1,true));
				mapswapIsShowed = true;

			}
			else
			{
				hideMapSwap();
			}
		}
		function showMapSwapEvent(evt:Event)
		{
			showMapSwap();
		}
		function hideMapSwap()
		{
			if (mapswapIsShowed)
			{
				freeTween();
				tweenList.unshift(new Tween (GameMenu.MapSwapPanel,"y",Strong.easeOut,-2,-272,1,true));
				mapswapIsShowed = false;
			}
		}
		function showRank()
		{
			if (! rankIsShowed)
			{
				hideAllPanel();
				loadRank();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.RankPanel,"y",Strong.easeOut,-272,-2,1,true));
				rankIsShowed = true;
			}
			else
			{
				hideRankPanel();
			}
		}
		function showRankEvent(evt:Event)
		{
			showRank();
		}
		function hideRankPanel()
		{
			if (rankIsShowed)
			{
				freeTween();
				tweenList.unshift(new Tween (GameMenu.RankPanel,"y",Strong.easeOut,-2,-272,1,true));
				rankIsShowed = false;
			}
		}
		function showPokedex()
		{
			if (! pokedexIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PokedexPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokedexIsShowed = true;
			}
			else
			{
				hidePokedexPanel();
			}
		}
		function showPokedexEvent(evt:Event)
		{
			showPokedex();
		}
		function hidePokedexPanel()
		{
			if (pokedexIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PokedexPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokedexIsShowed = false;
			}
		}
		function showPokemon()
		{
			if (! pokemonIsShowed)
			{
				GameMenu.PokemonPanel.render();
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PokemonPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokemonIsShowed = true;
			}
			else
			{
				hidePokemonPanel();
			}
		}
		function showPokemonEvent(evt:Event)
		{
			showPokemon();
		}
		function hidePokemonPanel()
		{
			if (pokemonIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PokemonPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokemonIsShowed = false;
			}
		}

		function showPVP()
		{
			if (! PVPIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PVPPanel,"y",Strong.easeOut,-300,-2,1,true));
				PVPIsShowed = true;
				GameMenu.PVPPanel.PVPAI.addEventListener(MouseEvent.CLICK,DoPVPAIEvent);
				GameMenu.PVPPanel.PVPPVP.addEventListener(MouseEvent.CLICK,DoPVPPVPEvent);
			}
			else
			{
				hidePVPPanel();
			}
		}
		function showPVPEvent(evt:Event)
		{
			showPVP();
		}
		function hidePVPPanel()
		{
			if (PVPIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PVPPanel,"y",Strong.easeOut,-2,-300,1,true));
				PVPIsShowed = false;
				GameMenu.PVPPanel.PVPAI.removeEventListener(MouseEvent.CLICK,DoPVPAIEvent);
				GameMenu.PVPPanel.PVPPVP.removeEventListener(MouseEvent.CLICK,DoPVPPVPEvent);
			}
		}
		//PVP
		function DoPVPPVPEvent(e:MouseEvent){
			if(String(Gamedt.Player.uid) != GameMenu.PVPPanel.PVPID.text){
				Gamedt.pvpws.sendRequest(GameMenu.PVPPanel.PVPID.text);
				GameMenu.pvpMask.visible=true;
				GameMenu.pvpMask.x=0;
				//endpvpbtn.pvpcancel.addEventListener(MouseEvent.CLICK,clearPVPEvent);
			}else{
				GameMenu.PVPPanel.PVPLog.text="ERROR";
			}
/*			GameMenu.PVPPanel.PVPLog.text="LOADING";
			SQLData.GetData("GetPVPPokemonList",DoPVPPVP,GameMenu.PVPPanel.PVPID.text);*/
		}
		public function clearPVP(){
			Gamedt.pvpws.clearItv();
			Gamedt.updatePlayer.Update("pvpask",0);
			Gamedt.updatePlayer.Update("pvpans",0);
			GameMenu.pvpMask.visible=false;		
		}
		public function clearPVPEvent(e:MouseEvent){
			clearPVP();
		}
		public function startPVP(pvpuid:Number){
			var newPVPBattlews:PVPLoadPokemonListws = new PVPLoadPokemonListws(pvpuid);
		}
		function DoPVPPVP(e:Event){
/*			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			if(AIPokemonList.length>0){				
				mapSwap(curentMap,1,false);
				BeforeBattle();
				BlankMC.addChild(new PVPBattle(AIPokemonList,GameMenu.PVPPanel.PVPID.text));	
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}*/
		}		
		function DoPVPAskEvent(e:MouseEvent){
/*				atutoPvpRespond = setInterval(checkMultiTab,200);//SQL
				GameMenu.PVPPanel.PVPLog.text="DANG CHO PHAN HOI";		*/	
		}
		function DoPVPAIEvent(e:MouseEvent){
			GameMenu.PVPPanel.PVPLog.text="LOADING";
			if(!Gamedt.pvpAI){
				Gamedt.pvpAI=true;
				var AIList:Array=new Array();
				var loadAIList:LoadPokemonList = new LoadPokemonList(Number(GameMenu.PVPPanel.PVPID.text));				
			}
			//SQLData.GetData("GetPVPPokemonList",DoPVPAI,GameMenu.PVPPanel.PVPID.text);
			
		}
		function DoPVPAI(e:Event){
/*			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			//var AIPokemonList:Array = newRivalPokemonList(AIPokemonListXML.lpkm,true);
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			SQLData.MainUI.Trace("AIPokemonList "+AIPokemonList.length);
			if(AIPokemonList.length>0){
				mapSwap(curentMap,1,false);
				BeforeBattle();
				BlankMC.addChild(new PokemonBattle(AIPokemonList,true));				
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}*/
		}
		function showItem()
		{
			if (! itemIsShowed)
			{
				GameMenu.ItemPanel.render();
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.ItemPanel,"y",Strong.easeOut,-272,-2,1,true));
				itemIsShowed = true;
			}
			else
			{
				hideItemPanel();
			}
		}
		function showItemEvent(evt:Event)
		{
			showItem();
		}
		function hideItemPanel()
		{
			if (itemIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.ItemPanel,"y",Strong.easeOut,-2,-272,1,true));
				itemIsShowed = false;
			}
		}

	
	}
}